package wholemusic.android;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;

import wholemusic.android.util.PageUtils;
import wholemusic.android.util.SongUtils;
import wholemusic.core.api.MusicApi;
import wholemusic.core.api.MusicApiFactory;
import wholemusic.core.model.Album;
import wholemusic.core.model.MusicLink;
import wholemusic.core.model.Song;

/**
 * Created by haohua on 2018/2/12.
 */

public class SongInfoActivity extends AppCompatActivity {
    public static final String EXTRA_SONG = "song";
    private Song mSong;

    private Button mDownloadButton;
    private Button mViewAlbumButton;

    private Button mDownloadAlbumButton;

    private TextView mSongNameTextView;
    private TextView mArtistNameTextView;
    private TextView mAlbumNameTextView;

    private View.OnClickListener mDownloadButtonOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new GetMusicLinkTask(mSong).execute();
        }
    };

    private View.OnClickListener mViewAlbumButtonOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(SongInfoActivity.this, R.string.view_album, Toast.LENGTH_SHORT).show();
        }
    };

    private View.OnClickListener mDownloadAlbumButtonOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DownloadAlbumTask(mSong).execute();
        }
    };

    private class GetMusicLinkTask extends AsyncTask<Void, Void, MusicLink> {
        private final Song song;

        public GetMusicLinkTask(Song song) {
            this.song = song;
        }

        @Override
        protected MusicLink doInBackground(Void... voids) {
            MusicApi api = MusicApiFactory.create(mSong.getMusicProvider());
            try {
                MusicLink result = api.getMusicLinkByIdSync(song.getSongId());
                return result;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(MusicLink musicLink) {
            if (musicLink != null) {
                SongUtils.downloadSong(SongInfoActivity.this, song, musicLink);
            }
        }
    }

    private class DownloadAlbumTask extends AsyncTask<Void, Void, Boolean> {
        private final Song song;

        public DownloadAlbumTask(Song song) {
            this.song = song;
        }

        @Override
        protected Boolean doInBackground(Void... voids) {
            MusicApi api = MusicApiFactory.create(song.getMusicProvider());
            String albumId = song.getAlbum().getAlbumId();
            try {
                Album album = api.getAlbumInfoByIdSync(albumId, true);
                if (album != null) {
                    final List<? extends Song> albumSongs = album.getSongs();
                    for (Song albumSong : albumSongs) {
                        SongUtils.downloadSong(SongInfoActivity.this, albumSong, albumSong.getMusicLink());
                    }
                    return true;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (result) {
                Toast.makeText(SongInfoActivity.this, R.string.task_added, Toast.LENGTH_SHORT).show();
                PageUtils.startDownloadManager(SongInfoActivity.this);
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 不要title bar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_song_info);

        mSongNameTextView = (TextView) findViewById(R.id.song_name_tv);
        mArtistNameTextView = (TextView) findViewById(R.id.artist_name_tv);
        mAlbumNameTextView = (TextView) findViewById(R.id.album_name_tv);
        mDownloadButton = (Button) findViewById(R.id.download_btn);
        mDownloadButton.setOnClickListener(mDownloadButtonOnClickListener);
        mViewAlbumButton = (Button) findViewById(R.id.view_album_btn);
        mViewAlbumButton.setOnClickListener(mViewAlbumButtonOnClickListener);
        mDownloadAlbumButton = (Button) findViewById(R.id.download_album_btn);
        mDownloadAlbumButton.setOnClickListener(mDownloadAlbumButtonOnClickListener);

        Intent intent = getIntent();
        mSong = (Song) intent.getSerializableExtra(EXTRA_SONG);

        mSongNameTextView.setText(mSong.getName());
        mArtistNameTextView.setText(SongUtils.getArtistsString(mSong));
        mAlbumNameTextView.setText(mSong.getAlbum().getName());
    }
}
